package com.tssf.car_manage.config;

import com.tssf.car_manage.filter.PowerFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;

@Configuration
public class WebMvcConfigurer implements org.springframework.web.servlet.config.annotation.WebMvcConfigurer {
    //视图控制器，所有的虚拟路径以及重定向全部写在这里
    //webMvcConfigurer是spring
    public void addViewControllers (ViewControllerRegistry viewControllerRegistry){
        viewControllerRegistry.addViewController("/").setViewName("login");
        viewControllerRegistry.addViewController("/register").setViewName("register");
        viewControllerRegistry.addViewController("/dashboard").setViewName("dashboard");
        viewControllerRegistry.addViewController("/CustomerList").setViewName("list");
        viewControllerRegistry.addViewController("/positioning").setViewName("positioning");

    }

    //将国际化配置注入到SpringMvc当中
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginHandlerInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/", "/login","/tologin", "/register","/sendCaptcha",
                        "/css/**", "/js/**", "/img/**",
                        "/fonts/**", "/error", "/favicon.ico",
                        "/api/**"
                );
    }
    @Bean
    public FilterRegistrationBean<PowerFilter> powerFilterRegistration() {
        FilterRegistrationBean<PowerFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new PowerFilter());
        // 只拦截特定的操作路径
        registration.addUrlPatterns(
                "/admin/add*",
                "/admin/edit*",
                "/admin/delete*",
                "/admin/update*"
        );
        registration.setName("powerFilter");
        registration.setOrder(1);
        return registration;
    }
}
